#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import Any

from cta.config.base_config import BaseConfig
from cta.interface.action.add_account.abstract_add_account_action import AbstractAddAccountAction
from web.domain.responsibility_chain_dto import ResponsibilityChainDto
from web.manager.log_manager import LogManager
from web.models.quant2_account import Quant2Account

Logger = LogManager.get_logger(__name__)


class AddQuant2AccountAction(AbstractAddAccountAction):
    """
    动量因子策略，添加账号
    """

    def __init__(self, successor=None):
        super().__init__(successor)

        self.quant2_account_list: list[Quant2Account] = list()

        quant2_account_1 = Quant2Account()
        quant2_account_1.id_ = 1
        quant2_account_1.account_name = '沃伦巴菲特'
        quant2_account_1.hold_commodity_future_number = 0
        quant2_account_1.commodity_future_assets = 0
        quant2_account_1.capital_assets = BaseConfig.Init_Funds
        quant2_account_1.total_assets = BaseConfig.Init_Funds
        quant2_account_1.total_open_commission = 0
        quant2_account_1.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_1)

        quant2_account_2 = Quant2Account()
        quant2_account_2.id_ = 2
        quant2_account_2.account_name = '杰西利弗莫尔'
        quant2_account_2.hold_commodity_future_number = 0
        quant2_account_2.commodity_future_assets = 0
        quant2_account_2.capital_assets = BaseConfig.Init_Funds
        quant2_account_2.total_assets = BaseConfig.Init_Funds
        quant2_account_2.total_open_commission = 0
        quant2_account_2.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_2)

        quant2_account_3 = Quant2Account()
        quant2_account_3.id_ = 3
        quant2_account_3.account_name = '彼得林奇'
        quant2_account_3.hold_commodity_future_number = 0
        quant2_account_3.commodity_future_assets = 0
        quant2_account_3.capital_assets = BaseConfig.Init_Funds
        quant2_account_3.total_assets = BaseConfig.Init_Funds
        quant2_account_3.total_open_commission = 0
        quant2_account_3.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_3)

        quant2_account_4 = Quant2Account()
        quant2_account_4.id_ = 4
        quant2_account_4.account_name = '本杰明格雷厄姆'
        quant2_account_4.hold_commodity_future_number = 0
        quant2_account_4.commodity_future_assets = 0
        quant2_account_4.capital_assets = BaseConfig.Init_Funds
        quant2_account_4.total_assets = BaseConfig.Init_Funds
        quant2_account_4.total_open_commission = 0
        quant2_account_4.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_4)

        quant2_account_5 = Quant2Account()
        quant2_account_5.id_ = 5
        quant2_account_5.account_name = '詹姆斯西蒙斯'
        quant2_account_5.hold_commodity_future_number = 0
        quant2_account_5.commodity_future_assets = 0
        quant2_account_5.capital_assets = BaseConfig.Init_Funds
        quant2_account_5.total_assets = BaseConfig.Init_Funds
        quant2_account_5.total_open_commission = 0
        quant2_account_5.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_5)

        quant2_account_6 = Quant2Account()
        quant2_account_6.id_ = 6
        quant2_account_6.account_name = '乔治索罗斯'
        quant2_account_6.hold_commodity_future_number = 0
        quant2_account_6.commodity_future_assets = 0
        quant2_account_6.capital_assets = BaseConfig.Init_Funds
        quant2_account_6.total_assets = BaseConfig.Init_Funds
        quant2_account_6.total_open_commission = 0
        quant2_account_6.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_6)

        quant2_account_7 = Quant2Account()
        quant2_account_7.id_ = 7
        quant2_account_7.account_name = '查尔斯亨利道'
        quant2_account_7.hold_commodity_future_number = 0
        quant2_account_7.commodity_future_assets = 0
        quant2_account_7.capital_assets = BaseConfig.Init_Funds
        quant2_account_7.total_assets = BaseConfig.Init_Funds
        quant2_account_7.total_open_commission = 0
        quant2_account_7.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_7)

        quant2_account_8 = Quant2Account()
        quant2_account_8.id_ = 8
        quant2_account_8.account_name = '李彪'
        quant2_account_8.hold_commodity_future_number = 0
        quant2_account_8.commodity_future_assets = 0
        quant2_account_8.capital_assets = BaseConfig.Init_Funds
        quant2_account_8.total_assets = BaseConfig.Init_Funds
        quant2_account_8.total_open_commission = 0
        quant2_account_8.total_close_commission = 0
        self.quant2_account_list.append(quant2_account_8)

    def exec(self, responsibility_chain_dto: ResponsibilityChainDto = None) -> Any:
        Logger.info("动量因子策略，添加账号")

        # 账号数量
        quant2_account_length: int = len(self.quant2_account_dao.find_all())
        # quant2_c_f_filter表中的记录数
        quant2_commodity_future_filter_length: int = len(self.quant2_commodity_future_filter_dao.find_all())

        # 计算账号应该的数量
        should_quant2_account_length: int = 0
        if quant2_commodity_future_filter_length == 0:
            should_quant2_account_length = 1
        elif quant2_commodity_future_filter_length % 10 != 0:
            should_quant2_account_length = int(quant2_commodity_future_filter_length / 10) + 1
        else:
            should_quant2_account_length = int(quant2_commodity_future_filter_length / 10)

        # 准备需要插入的账号
        insert_quant2_account_list: list[Quant2Account] = list()
        for index, value in enumerate(self.quant2_account_list):
            if index + 1 > quant2_account_length and index + 1 <= should_quant2_account_length:
                insert_quant2_account_list.append(self.quant2_account_list[index])
                Logger.info("现在的账号数量为[%s]，quant2_c_f_filter表的记录数为[%s], 插入账号[%s]",
                            quant2_account_length, quant2_commodity_future_filter_length, self.quant2_account_list[index].account_name)

        # 插入记录
        if insert_quant2_account_list is not None and len(insert_quant2_account_list) > 0:
            self.quant2_account_dao.save_batch(insert_quant2_account_list)

        super().next(responsibility_chain_dto)
